<?php
namespace app\common\lib;
class SimpleToTraditional
{
    private $utf8_gb2312;
    private $utf8_big5;
    private $data;
    function __construct($data){
        $this->utf8_gb2312 = $data['sim'];
        $this->utf8_big5 = $data['tra'];
    }
   public function gb2312_big5($str) {
        $str_t = '';
        $len = strlen($str);
        $a = 0;
        while ($a < $len){
            if (ord($str{$a})>=224 && ord($str{$a})<=239){
                if (($temp = strpos( $this->utf8_gb2312, $str{$a} . $str{$a+1} . $str{$a+2})) !== false){
                    $str_t .= $this->utf8_big5{$temp} . $this->utf8_big5{$temp+1} . $this->utf8_big5{$temp+2};
                    $a += 3;
                    continue;
                }
            }
            $str_t .= $str{$a};
            $a += 1;
        }
        return $str_t;
    }
    public function big5_gb2312($str) {
        $str_t = '';
        $len = strlen($str);
        $a = 0;
        while ($a < $len){
            if (ord($str{$a})>=224 && ord($str{$a})<=239){
                if (($temp = strpos( $this->utf8_big5, $str{$a} . $str{$a+1} . $str{$a+2})) !== false){
                    $str_t .= $this->utf8_gb2312{$temp} . $this->utf8_gb2312{$temp+1} . $this->utf8_gb2312{$temp+2};
                    $a += 3;
                    continue;
                }
            }
            $str_t .= $str{$a};
            $a += 1;
        }
        return $str_t;
    }
}
